<?php
class App_Module_Admin_Component_Users_Ctrl_Http_Web extends App_Module_Admin_Component_Users_Ctrl_Http
{
    private $_id;
    protected $_page = 1;

    protected function ControllerInit()
    {
    	parent::ControllerInit();

        $this->_id = (int)Mozg::Request('http')->get('id');

    	if (Mozg::Request('http')->hasQuery('page'))
    	{
			$this->_page = (int)Mozg::Request('http')->getQuery('page');
			if ($this->_page != Mozg::Request('http')->getCookie('page'))
			{
				$scope = Mozg_Router::Get('http')->Create(array('module' => 'admin', 'ctrl' => 'users'));
				Mozg::Request('http')->SetCookie('page', $this->_page, 0, $scope);
			}
    	}
    	else
    	{
    		$this->_page = (int)Mozg::Request('http')->getCookie('page');
    	}
    }

    protected function PrimaryActionInit($basicActionName)
    {
        Mozg::Layout('admin')->SetMenuActiveElement('access/users');
    }
    /**
		экшен по-умолчанию
	*/
    public function PrAction ()
    {
        $this->listPr();
    }
    public function addPrAction ($user = null)
    {
        if (!$user)
        {
	        if ($this->_id)
	        {
				$user = Mozg_Mapper::Get('Users')->GetOne(array('id' => $this->_id, 'groups' => Mozg::User()->GetAvailableGroups()));
				if (!$user)
				{
					$this->_id = 0;
				}
	        }

	   		if (!$this->_id) $user = Mozg_Mapper::Get('Users')->GetEntity();
        }

        $t = $this->GetTpl('Editform');
        Mozg::Response('http')->SetBody($t->GetForm($user));
    }

    public function savePrAction ()
    {
        if ($this->_id)
        {
			$user = Mozg_Mapper::Get('Users')->GetOne(array('id' => $this->_id, 'groups' => Mozg::User()->GetAvailableGroups()));
			if (!$user)
			{
				$this->_id = 0;
			}
        }

   		if (!$this->_id) $user = Mozg_Mapper::Get('Users')->GetEntity();

   		$this->GetTpl('Editform')->AddPostData($user, (array)Mozg::Request('http')->getPost('user'));

   		$user->SetGroupId((int)Mozg::Request('http')->getPost('group_id'));
        $user->SetGrantedGroups((array)Mozg::Request('http')->getPost('grant_groups'));

        if (Mozg_Mapper::Get('Users')->Save($user))
        {
            Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('DATA_SAVED'));
            Mozg_Router::Get('http')->Redirect(array('module' => 'admin', 'ctrl' => 'users'));
        }
        else
        {
			if (!$user->Validator()->GetState())
        	{
        		Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('INVALID_DATA'), MESSAGE_ERROR);
        	}
        	else
        	{
	            Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('DATA_NOT_SAVED'), MESSAGE_ERROR);
        	}

        	$this->addPr($user);
            return;
        }
    }

    public function delPrAction ()
    {
        if (!Mozg_Mapper::Get('Users')->Delete(array('id' => $this->_id, 'groups' => Mozg::User()->GetAvailableGroups())))
        {
        	if (!($message = Mozg_Mapper::Get('Users')->GetErrorMessage())) $message = Mozg_Lang::_('USER_WAS_NOT_FOUND');
            Mozg::Lib('messanger')->createMessage($message, MESSAGE_ERROR);
        }
        else
        {
        	Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('USER_DELETED'));
        }

        Mozg_Router::Get('http')->Redirect(array('module' => 'admin', 'ctrl' => 'users'));
    }

    public function listPrAction ()
    {
        $t = $this->GetTpl('List');
        Mozg::Response('http')->SetBody($t->GetList($this->_page));
    }

    public function editprofilePrAction ($user = null)
    {
        if (!$user)
        {
			$user = clone Mozg::User();
			if (!$user) return $this->profilePr();
        }

        $t = $this->GetTpl('Editprofile');
        Mozg::Response('http')->SetBody($t->GetForm($user));
    }

    public function saveprofilePrAction ()
    {
    	$user = clone Mozg::User();
    	if (!$user) return $this->profilePr();

   		$this->GetTpl('Editprofile')->AddPostData($user, (array)Mozg::Request('http')->getPost('user'));

		if (Mozg_Mapper::Get('Users')->Save($user))
        {
            Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('PROFILE_SAVED'));
            Mozg_Router::Get('http')->Redirect(array('module' => 'admin', 'ctrl' => 'users', 'action' => 'profile'));
            return;
        }
        else
        {
        	if (Mozg_Mapper::Get('Users')->IsError())
        	{
        		Mozg::Lib('messanger')->createMessage(Mozg_Mapper::Get('Users')->GetErrorMessage(), MESSAGE_ERROR);
        	}
        	else
        	{
	            Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('DATA_WAS_NOT_SAVED. PLEASE_TRY_AGAIN.'), MESSAGE_ERROR);
        	}

        	$this->editprofilePr($user);
            return;
		}
    }

    public function profilePrAction ()
    {
        $t = $this->GetTpl('Web');
        Mozg::Response('http')->SetBody($t->ViewProfile(Mozg::User()));
    }

    public function userstatus()
    {
    	if (!Mozg::User()->GetId())
    	{
    		$t = $this->GetTpl('Web');
    		return $t->SmallLoginForm();
    	}

        $t = $this->GetTpl('Web');
        $t->user = Mozg::User();
        return $t->UserStatus();
    }
}